package com.heima.article.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.heima.apis.wemedia.IWemediaClient;
import com.heima.article.mapper.ApArticleMapper;
import com.heima.article.service.HotArticleService;
import com.heima.common.constants.ArticleConstants;
import com.heima.common.redis.CacheService;
import com.heima.model.article.pojos.ApArticle;
import com.heima.model.article.vos.HotArticleVo;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.wemedia.pojos.WmChannel;
import lombok.extern.slf4j.Slf4j;
import org.joda.time.DateTime;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

@Service
@Slf4j
public class HotArticleServiceImpl implements HotArticleService {
    @Autowired
    private ApArticleMapper apArticleMapper;

    @Autowired
    private IWemediaClient iWemediaClient;

    @Autowired
    private CacheService cacheService;

    @Override
    public void comHotArticle() {
        Date date = DateTime.now().minusYears(2).toDate();
        //查询前五天的文章数据
        List<ApArticle> last5Date = apArticleMapper.getLast5Date(date);
        List<HotArticleVo> hotArticleVoList = new ArrayList<>();
        System.out.println(last5Date);
        //计算文章的分值
        if (last5Date != null && last5Date.size() > 0) {
            for (ApArticle article : last5Date) {
                HotArticleVo hotArticleVo = new HotArticleVo();
                BeanUtils.copyProperties(article, hotArticleVo);
                Integer score = computeScore(article);
                hotArticleVo.setScore(score);
                hotArticleVoList.add(hotArticleVo);
            }
        }
        //为每个频道缓存30条分值较高的文章
        ResponseResult channels = iWemediaClient.getChannels();
        if (channels.getCode() == 200) {
            //转字符串
            String s = JSONObject.toJSONString(channels.getData());
            //格式化成list集合
            List<WmChannel> wmChannels = JSONObject.parseArray(s, WmChannel.class);
            if (wmChannels != null && wmChannels.size() > 0) {
                for (WmChannel wmChannel : wmChannels) {
                    List<HotArticleVo> collect = hotArticleVoList.stream().filter(z -> z.getChannelId().
                            equals(wmChannel.getId())).collect(Collectors.toList());
                    extracted(collect, ArticleConstants.HOT_ARTICLE_FIRST_PAGE + wmChannel.getId());
                }
            }
        }

        //推荐数据
        extracted(hotArticleVoList, ArticleConstants.HOT_ARTICLE_FIRST_PAGE + ArticleConstants.DEFAULT_TAG);
    }

    /**
     * 取出文章存入缓存
     *
     * @param collect
     */
    private void extracted(List<HotArticleVo> collect, String key) {
        //给文章进行排序,取30条分值较高的文章存入redis
        collect = collect.stream().sorted(Comparator.comparing(HotArticleVo::getScore).reversed())
                .collect(Collectors.toList());
        if (collect.size() > 30) {
            collect = collect.subList(0, 30);
        }
        //存入缓存
        cacheService.set(key,
                JSON.toJSONString(collect));
    }

    /**
     * 计算分数
     *
     * @param article
     * @return
     */
    private Integer computeScore(ApArticle article) {
        Integer score = 0;
        if (article.getLikes() != null) {
            score += article.getLikes() * ArticleConstants.HOT_ARTICLE_LIKE_WEIGHT;
        }
        if (article.getViews() != null) {
            score += article.getViews();
        }
        if (article.getComment() != null) {
            score += article.getComment() * ArticleConstants.HOT_ARTICLE_COMMENT_WEIGHT;
        }
        if (article.getCollection() != null) {
            score += article.getCollection() * ArticleConstants.HOT_ARTICLE_COLLECTION_WEIGHT;
        }
        return score;
    }
}
